enrichGO
library(ggrepel)
library(ggplot2)
result <- enrichGO

library("clusterProfiler")
library("org.Hs.eg.db")
gene_list <- upgene$Gene_symbol


result <- enrichGO(gene = gene_list,
                   OrgDb = org.Hs.eg.db,
                   keyType = "SYMBOL",
                   ont = "ALL",#表示对所有的三个本体进行功能富集分析
                   pAdjustMethod = "fdr",
                   qvalueCutoff = 0.01,
                   readable = TRUE)
#ont:这个参数用于指定富集分析所涉及的本体（ontology）
#BP代表生物学过程 biological process
#MF代表分子功能 cellular component
###这就富集完成了。。。
write.table(result.file= "GOresult.txt",sep= "\t",quote = F,row,names = F)

#绘图 barplot条状图
library(ggplot2)
barplot(result,showCategory = 30)
barplot(result,x = "GeneRatio", color = "p.adjust",
         showCategory = 10,
        split = "ONTOLOGY")+
  facet_grid(ONTOLOGY~.,scales = 'free')

#气泡图
dotplot(result.x = "GeneRatio",color = "p.adjust", size = "Count",
        showCatgory = 5,
        split = "ONTOLOGY") + 
  facet_grid(ONTOLOGY~.,scales = 'free')


library(tidyverse)
eGo = read.table("GOresult.txt",header = TRUE,sep = "\t",quote = "")
eGo <- separate(data = eGo,col = GeneRatio,into = c("GR1","GR2"),sep = "/")
eGo <- separate(data = eGo,col = BgRatio,into = c("BR1","BR@"),sep = "/")
eGo <- mutate(eGo,enrichment_factor = (as.numeric(GR1)/as.numeric(GR2))/(as.numeric(BR1)/as.numeric(BR2)))

library(ggplot2)
p <- ggplot(eGo,aes(enrichment_factor,description))+
  geom_point(aes(size = Count,color = qvalue,shape = ONTOLOGY))+
  scale_color_gradient(low = "red",high = "green") +
  labs(color = "pvalue",size = "Count",shape = "GO enrichment") +
  theme_bw()
p

p <- ggplot(eGo,aes(enrichment_factor,description))+
  geom_point(aes(size = Count,color = -1*log10(pvalue),shape = ONTOLOGY))+
  scale_color_gradient(low = "red",high = "green") +
  labs(color = expression(-log[10](p_value)),size = "Count",shape = "Ontology",
       x= "Enrichment Factor",y = "GO term",title = "GO enrichment") +
  theme_bw()
p

p +facet_wrap( ~ ONTOLOGY) #竖着分屏

p +facet_wrap( ~ ONTOLOGY,nocl = 1,scales = 'free')#横着分屏




